from django.apps import AppConfig


class AssetsConfig(AppConfig):
    name = 'assets'
    verbose_name = '资产'

    def ready(self):
        try:
            from .models import Company, Hosts, Accessories
            if not Company.objects.all():
                hosts = Hosts.objects.all()
                accessories = Accessories.objects.all()
                for host in hosts:
                    if host.location:
                        host.company, _ = Company.objects.get_or_create(name=host.location)
                for a in accessories:
                    if a.location:
                        a.company, _ = Company.objects.get_or_create(name=a.location)
                Hosts.objects.bulk_update(hosts, ["company"])
                Accessories.objects.bulk_update(hosts, ["company"])
        except Exception as e:
            print(e)
            pass
